<html>
<head><meta charset="utf-8"><title>Concurrent bounded free list · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Concurrent.20bounded.20free.20list.html">Concurrent bounded free list</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239381351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Concurrent%20bounded%20free%20list/near/239381351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Concurrent.20bounded.20free.20list.html#239381351">(May 19 2021 at 07:53)</a>:</h4>
<p>Hello!<br>
In allocation heavy scenario, I sometime need some kind of  "bounded free list".<br>
For objects that are expensive to create and/or drop, instead of creating new ones all the time, I usually reset them and store them in a Vec basically just doing push to append to the free list (unless there are already enough objects in the there) and pop (to retrieve an available object if there is one).<br>
When when it comes to multi-threaded scenario, I wonder if there is a better / more performant approach to just blindly using a Mutex&lt;Vec&lt;T&gt;&gt;?</p>



<a name="239383426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Concurrent%20bounded%20free%20list/near/239383426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kestrer <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Concurrent.20bounded.20free.20list.html#239383426">(May 19 2021 at 08:11)</a>:</h4>
<p>You may want to use an MPMC queue such as <a href="https://docs.rs/concurrent-queue">https://docs.rs/concurrent-queue</a> or <a href="https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.ArrayQueue.html">https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.ArrayQueue.html</a>.</p>



<a name="239384029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Concurrent%20bounded%20free%20list/near/239384029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Concurrent.20bounded.20free.20list.html#239384029">(May 19 2021 at 08:16)</a>:</h4>
<p>Ah very interesting thanks! Crossbeam should already be <em>somewhere</em> in my dependency tree anyway :)<br>
Have there been performance analysis of those two crates compared to a classic Mutex lock-ing?<br>
In my scenario, I actually don't do much concurrent access, but I need to provide a thread-safe API to another project and therefore; even though it's not really used in a multi-threaded environment <em>yet</em> I need all the thread-safety I can get, but without sacrificing performances ideally</p>



<a name="239395115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Concurrent%20bounded%20free%20list/near/239395115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Concurrent.20bounded.20free.20list.html#239395115">(May 19 2021 at 09:51)</a>:</h4>
<p>A mutex should be quite fast as long as it is uncontended. An atomic RMW instruction per lock and unlock, no syscalls.</p>



<a name="239395643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Concurrent%20bounded%20free%20list/near/239395643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Concurrent.20bounded.20free.20list.html#239395643">(May 19 2021 at 09:55)</a>:</h4>
<p>Ok that's good to hear. There is probably no point in using advanced current queues yet then.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>